package com.yang.service.adminimpl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.*;
import com.yang.bean.common.CommonData;
import com.yang.bean.dhr.MarketRoleOptionsVo;
import com.yang.bean.dhr.MarketRoleVo;
import com.yang.bean.yvo.qunxianvo.*;
import com.yang.mapper.MarketPermissionMapper;
import com.yang.mapper.MarketRoleMapper;
import com.yang.mapper.MarketShouQuanMapper;
import com.yang.service.admin.AdminRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.lang.model.element.QualifiedNameable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author dinghongrui
 * @create 2022/09/07 16:10
 */
@Service
public class AdminRoleServiceImpl implements AdminRoleService {
    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Override
    public CommonData<MarketRole> selectAllRole(Integer page, Integer limit, String sort, String order, String name) {
        PageHelper.startPage(page, limit);
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andEnabledEqualTo(true);
        criteria.andDeletedEqualTo(false);


        example.setOrderByClause(sort + " " + order);
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        PageInfo<MarketRole> pageInfo = new PageInfo<>(marketRoles);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public CommonData<MarketRoleOptionsVo> selectAllRoleOptions() {
        MarketRoleExample example = new MarketRoleExample();
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        List<MarketRoleOptionsVo> marketRoleOptionsVos = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            MarketRoleOptionsVo marketRoleOptionsVo = new MarketRoleOptionsVo();
            marketRoleOptionsVo.setValue(marketRole.getId());
            marketRoleOptionsVo.setLabel(marketRole.getName());
            marketRoleOptionsVos.add(marketRoleOptionsVo);
        }
        PageInfo<MarketRoleOptionsVo> pageInfo = new PageInfo<>(marketRoleOptionsVos);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public MarketRoleVo createRole(MarketRole marketRole) {
        Date date = new Date();
        marketRole.setAddTime(date);
        marketRole.setUpdateTime(date);
        marketRoleMapper.insertSelective(marketRole);
        MarketRoleVo marketRoleVo = new MarketRoleVo();
        marketRoleVo.setId(marketRole.getId());
        marketRoleVo.setName(marketRole.getName());
        marketRoleVo.setDesc(marketRole.getDesc());
        marketRoleVo.setAddTime(marketRole.getAddTime());
        marketRoleVo.setUpdateTime(marketRole.getUpdateTime());

        return marketRoleVo;

    }

    @Override
    public void updateRole(MarketRole marketRole) {
        Date date = new Date();
        marketRole.setUpdateTime(date);
        marketRoleMapper.updateByPrimaryKeySelective(marketRole);
    }

    @Override
    public void deleteRole(MarketRole marketRole) {
        marketRole.setEnabled(false);
        marketRole.setDeleted(true);
        marketRoleMapper.updateByPrimaryKey(marketRole);
//        marketRoleMapper.deleteByPrimaryKey(marketRole.getId());
    }


    @Autowired
    MarketShouQuanMapper marketShouQuanMapper;
    @Autowired
    MarketPermissionMapper marketPermissionMapper;

    /**
     * @description: 授权的查询
     * [roleId]
     * @return: com.yang.bean.yvo.qunxianvo.PermissionVO
     * @author: 杨庆
     * @time: 2022.9.12 10:52
     */
    @Override
    public PermissionVO selectPermissions(Integer roleId) {
        PermissionVO permissionVO = new PermissionVO();

        //查询所有的一级
        MarketShouQuanExample marketShouQuanExample = new MarketShouQuanExample();
        MarketShouQuanExample.Criteria criteria = marketShouQuanExample.createCriteria();
        criteria.andLevelEqualTo("L1");
        List<MarketShouQuan> marketShouQuans = marketShouQuanMapper.selectByExample(marketShouQuanExample);
        //TOdo 一级的类目添加到permissionVO  可不可以先放入到一个map的集合在去获取；
        ArrayList<SystemPermissions> list = new ArrayList<>();
        for (MarketShouQuan marketShouQuan : marketShouQuans) {
            SystemPermissions s1 = new SystemPermissions();
            s1.setId(marketShouQuan.getId());
            s1.setLabel(marketShouQuan.getLabel());
            Integer ids = marketShouQuan.getIds();
            //TOdo 二级的  根据pid
            MarketShouQuanExample marketShouQuanExample1 = new MarketShouQuanExample();
            MarketShouQuanExample.Criteria criteria1 = marketShouQuanExample1.createCriteria();
            criteria1.andLevelEqualTo("L2");
            criteria1.andPidEqualTo(ids);
            List<MarketShouQuan> marketShouQuans1 = marketShouQuanMapper.selectByExample(marketShouQuanExample1);
            ArrayList<Children> list2 = new ArrayList<>();
            for (MarketShouQuan shouQuan : marketShouQuans1) {
                Children children = new Children();
                children.setId(shouQuan.getId());
                children.setLabel(shouQuan.getLabel());

                Integer ids1 = shouQuan.getIds();
                //TODO 三级L3
                List<Children1> children1 = children.getChildren();
                ArrayList<Children1> list3 = new ArrayList<>();
                MarketShouQuanExample marketShouQuanExample2 = new MarketShouQuanExample();
                MarketShouQuanExample.Criteria criteria2 = marketShouQuanExample2.createCriteria();
                criteria2.andLevelEqualTo("L3");
                criteria2.andPidEqualTo(ids1);
                List<MarketShouQuan> marketShouQuans2 = marketShouQuanMapper.selectByExample(marketShouQuanExample2);
                for (MarketShouQuan quan : marketShouQuans2) {
                    Children1 children111 = new Children1();
                    children111.setApi(quan.getApi());
                    children111.setLabel(quan.getLabel());
                    children111.setId(quan.getId());
                    list3.add(children111);
                }
                children.setChildren(list3);
                list2.add(children);
            }
            s1.setChildren(list2);
            list.add(s1);
        }

        permissionVO.setSystemPermissions(list);

        //TODO 当前角色的权限列表 可以通过perssion来进行获取
        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria1 = example.createCriteria();
        criteria1.andRoleIdEqualTo(roleId);
        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(example);
        ArrayList<String> list4 = new ArrayList<>();

        for (MarketPermission marketPermission : marketPermissions) {
            String permission = marketPermission.getPermission();
            list4.add(permission);
        }
        permissionVO.setAssignedPermissions(list4);

        return permissionVO;
    }


    /**
    * @description: 修改用户的权限
    [perPostBO]
    * @return: void
    * @author: 杨庆
    * @time: 2022.9.12 14:36
     */
    @Override
    public void updatePermissions(PerPostBO perPostBO) {
        //TODO 根据用户的角色 先删除
        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();
        int roleId = perPostBO.getRoleId();
        if (roleId != 0) {
            criteria.andRoleIdEqualTo(roleId);
        }
        marketPermissionMapper.deleteByExample(example);
        //再插入
        List<String> permissions = perPostBO.getPermissions();
        for (String permission : permissions) {
            MarketPermission marketPermission = new MarketPermission();
            marketPermission.setAddTime(new Date());
            marketPermission.setRoleId(roleId);
            marketPermission.setPermission(permission);
            marketPermissionMapper.insertSelective(marketPermission);
        }
    }
}




